package com.example.ch02_student.adminsDo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ch02_student.R;
import com.example.ch02_student.entity.Student;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StudentGradeUpdate extends AppCompatActivity {
private EditText sno,cno,grade;
private OkHttpClient  okHttpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_grade_update);
        init();
        okHttpClient=new OkHttpClient();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            sno.setText(bundle.getString("sno"));
        }

    }

    //更新学生成绩按钮
    public void  updateStudentGrade(View view){
        String n=sno.getText()+"";
        String c=cno.getText()+"";
        int g=Integer.parseInt(grade.getText()+"");
        Request build = new Request.Builder()
                .url(UrlBase.URL_BASE + "/student/updateStuGrade?sno="+n+"&cno="+c+"&score="+g)
                .build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                ResultVo resultVo = new Gson().fromJson(string, ResultVo.class);
                if (resultVo.getCode()==1000){
                    Looper.prepare();
                    Toast.makeText(StudentGradeUpdate.this,"修改成功 ",Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }else {
                    System.err.println("error");
                }
            }
        });

    }

    //返回按钮
    public void fanhui(View view){
        startActivity(new Intent(StudentGradeUpdate.this, StudentInfo.class));
    }

    public void init(){
        sno=findViewById(R.id.no);
        cno=findViewById(R.id.cno);
        grade=findViewById(R.id.grade);
    }
}
